package cn.hesion.friend;
/**
 * ClassName: FriendTableDemo <br/>
 * Description: <br/>
 * date: 2021/3/13 9:28<br/>
 *
 * @author Hesion<br />
 * @version
 * @since JDK 1.8
 */

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.ArrayList;

/**
 * 表格操作类
 *
 * @author: hesion
 * @create: 2021-03-13 09:28
 **/
public class FriendTableDemo {
    //创建两个方法，一个创建表格，一个添加数据，以此来熟悉API的操作

    static Configuration conf = null;
    static Connection conn = null;

    //静态代码块来加载连接HBase的代码
    static {
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","linux121,linux122");
        conf.set("hbase.zookeeper.property.clientPort","2181");

        try {
            conn = ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void CreateFriendRelationTable() throws IOException {
        Admin admin = conn.getAdmin();
        //创建表描述器
        HTableDescriptor relation = new HTableDescriptor(TableName.valueOf("relation"));
        //创建列族描述器
        HColumnDescriptor cf = new HColumnDescriptor("friends");
        HTableDescriptor hTableDescriptor = relation.addFamily(cf);
        admin.createTable(hTableDescriptor);

        System.out.println("创建relation表成功");
        //关闭
        admin.close();

    }

    //初始化表格数据
    public void initRelationData() throws IOException {
        Table relation = conn.getTable(TableName.valueOf("relation"));

        //插入uid为1的用户
        Put uid1 = new Put(Bytes.toBytes("uid1"));
        uid1.addColumn(Bytes.toBytes("friends"),Bytes.toBytes("uid2"),Bytes.toBytes("uid2"));
        uid1.addColumn(Bytes.toBytes("friends"),Bytes.toBytes("uid3"),Bytes.toBytes("uid3"));
        uid1.addColumn(Bytes.toBytes("friends"),Bytes.toBytes("uid4"),Bytes.toBytes("uid4"));

        //插入uid为2用户
        final Put uid2 = new Put(Bytes.toBytes("uid2"));
        uid2.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid1"), Bytes.toBytes("uid1"));
        uid2.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid3"), Bytes.toBytes("uid3"));
        uid2.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid4"), Bytes.toBytes("uid4"));

        ArrayList<Put> puts = new ArrayList<>();
        puts.add(uid1);
        puts.add(uid2);
        //执行写入
        relation.put(puts);
        System.out.println("数据初始化成功");
    }


    //执行删除操作
    //可以通过shell去演示
    public void delteFriends(String uid,String friend) throws IOException {
        Table relation = conn.getTable(TableName.valueOf("relation"));

        Delete delete = new Delete(Bytes.toBytes(uid));
        delete.addColumn(Bytes.toBytes("friends"),Bytes.toBytes(friend));

        relation.delete(delete);
    }


    public static void main(String[] args) throws IOException {
        FriendTableDemo friendTableDemo = new FriendTableDemo();
//        friendTableDemo.CreateFriendRelationTable();
        friendTableDemo.initRelationData();

    }
}
